﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UcAsp.Net.Extract
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.MainMenuStrip = hiddenMax;
            this.MdiChildActivate += new EventHandler(Form1_MdiChildActivate);
            this.IsMdiContainer = true;
            

        }

        void Form1_MdiChildActivate(object sender, EventArgs e)
        {
           Form form= this.ActiveMdiChild;
           foreach (Controls.TabButton btn in fpnlButton.Controls)
            {
                if (form != null)
                {
                    if (btn.Name == form.Name)
                    {
                        btn.IsActive = true;
                    }
                    else
                    {
                        btn.IsActive = false;
                    }
                    btn.Invalidate();
                }
            }
           
        }

        void f_FormClosed(object sender, FormClosedEventArgs e)
        {
            Form f = (Form)sender;
            fpnlButton.Controls.Remove(fpnlButton.Controls.Find(f.Name,true)[0]);           
            f.FormClosed-= new FormClosedEventHandler(f_FormClosed);
        }

        private void ShowSingleWindow(Type type)
        {

            foreach (Form f in this.MdiChildren)
            {
                if (f.GetType() == type)
                {
                    f.Activate();
                    return;
                }
            }
            
            Form frm = type.Assembly.CreateInstance(type.ToString()) as Form;
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
            fpnlButton.Controls.Clear();
            foreach (Form f in this.MdiChildren)
            {
                Controls.TabButton btn = new Controls.TabButton();
                btn.Text = f.Name;
                btn.Name = f.Name;
                btn.Width = 200;
                btn.Height = 25;
                btn.IsActive =false;
                btn.BackColor = Color.Transparent;
                btn.Close += new EventHandler(btn_Close);
                btn.Click += new EventHandler(btn_Click);
                fpnlButton.Controls.Add(btn);
            }
            Controls.TabButton fbtn = (Controls.TabButton)fpnlButton.Controls.Find(frm.Name, true)[0];
          fbtn.IsActive = true;
          fbtn.Invalidate();
        }

        void btn_Click(object sender, EventArgs e)
        {

            Controls.TabButton tb = (Controls.TabButton)sender;
            foreach (Form f in this.MdiChildren)
            {
                if (f.Name == tb.Name)
                {
                    f.Activate();
                    return;
                }
            }
        }

        void btn_Close(object sender, EventArgs e)
        {
            Controls.TabButton tb = (Controls.TabButton)sender;
            foreach (Form f in this.MdiChildren)
            {
                if (f.Name == tb.Name)
                {
                    f.Close();
                    fpnlButton.Controls.Remove(tb);
                    return;
                }
            }
           
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            ShowSingleWindow(typeof(FrmBase));
        }

        private void customControl11_Click(object sender, EventArgs e)
        {
            MessageBox.Show("ss");
        }

        private void tabButton1_Click(object sender, EventArgs e)
        {
            ShowSingleWindow(typeof(Form2));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.SizeChanged += new EventHandler(Form1_SizeChanged);
#if DEBUG
            MessageBox.Show("ss");
#else
             MessageBox.Show("MMM");
#endif
        }

        void Form1_SizeChanged(object sender, EventArgs e)
        {

            foreach (Control ctl in this.Controls)
            {
                if (ctl.GetType().ToString() == "System.Windows.Forms.MdiClient")
                {
                    
                    System.Windows.Forms.MdiClient m = (System.Windows.Forms.MdiClient)ctl;
                    
                    Rectangle r = m.ClientRectangle;
                    Graphics g = m.CreateGraphics();
                    //Pen p=new Pen(
                    g.DrawRectangle(new Pen(Brushes.Black, (float)r.Width), r);
                    break;

                }
            }
        }
    }
}
